package com.az.ducbt.cliphot;

import java.io.UnsupportedEncodingException;

public class CryptText {

	public CryptText() {
		super();
	}
	public static String decode(String value) {
        if ((value != null) && (value.length() > 0)) {
            int len = value.length();
            int padding = 0;
            for (int i = len - 1; value.charAt(i) == '='; i--) {
                padding++;
            }
            StringBuffer buff = new StringBuffer((len * 6 >> 3) - padding);
            int index = 0;
            for (int i = 0; i < len; i += 4) {
                int chunk = (decode(value.charAt(i)) << 18) + (decode(value.charAt(i + 1)) << 12) + (decode(value.charAt(i + 2)) << 6) + decode(value.charAt(i + 3));
                for (int j = 0; (j < 3) && (index + j < buff.capacity()); j++) {
                    buff.append((char) (chunk >> 8 * (2 - j) & 0xFF));
                }
                index += 3;
            }
            try {
                return new String(buff.toString().getBytes(), "ISO-8859-1");
            } catch (UnsupportedEncodingException ex) {
                System.out.print("Co loi");
            }
        }
        return "";
    }

    public static int decode(int i) {
        if ((i >= 65) && (i <= 90)) {
            return i - 65;
        }
        if ((i >= 97) && (i <= 122)) {
            return 26 + i - 97;
        }
        if ((i >= 48) && (i <= 57)) {
            return 52 + i - 48;
        }
        if (i == 43) {
            return 62;
        }
        return 63;
    }
}
